<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:u="livcos.org/ns/cosmos/update"
  xmlns:m="livcos.org/ns/map"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="c job u m f xs">
  
  <xsl:import href="../../web/style/base.xsl"/>
  
  <xsl:param name="url" select="'/brunof.net/data/external/NetVouz/dreamair_private'"/>
  <xsl:param name="map" select="'/brunof.net/data/map/NetVouz_private'"/>
  
  <xsl:template match="/">
    <xsl:variable name="data">
      <xsl:apply-templates select="document($url)/*" mode="prep"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="doc-available($map)">
        <u:update ref="{$map}" scope="content">
          <xsl:apply-templates select="$data//m:item"/>
        </u:update>
      </xsl:when>
      <xsl:otherwise>
        <u:insert ref="{$map}" create-path="1">
          <map xmlns="livcos.org/ns/map" xmlns:doc="livcos.org/ns/doc">
            <xsl:apply-templates select="$data//m:item"/>
          </map>
        </u:insert>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="m:item">
    <xsl:text>&#x0A;</xsl:text>
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="m:rels">
    <xsl:copy>
      <xsl:attribute name="id">rels_<xsl:number level="any" count="m:rels"/></xsl:attribute>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="m:rel">
    <xsl:copy>
      <xsl:attribute name="id">rel_<xsl:number level="any" count="m:rel"/></xsl:attribute>
      <xsl:attribute name="ref">
        <xsl:text>#</xsl:text>
        <xsl:value-of select="*/@id"/>
        <xsl:if test="not(*)">
          <xsl:value-of select="../../../../../@id"/>
        </xsl:if>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="CATEGORY" mode="prep">
    <item xmlns="livcos.org/ns/map">
      <xsl:attribute name="id">item_<xsl:number level="any" count="CATEGORY | BOOKMARK"/></xsl:attribute>
      <xsl:apply-templates select="createDate | editDate" mode="#current"/>
      <label>
        <xsl:value-of select="name"/>
        <xsl:if test="normalize-space(name) = ''">
          <xsl:value-of select="if (privat = 'true') then 'Private' else 'Public'"/>
        </xsl:if>
      </label>
      <rels group="main">
        <xsl:apply-templates select="CATEGORY | BOOKMARK" mode="prep_rel"/>
      </rels>
      <xsl:if test="../../..">
        <rels group="sys">
          <rel/>
        </rels>
      </xsl:if>
    </item>
  </xsl:template>
  
  <xsl:template match="CATEGORY | BOOKMARK" mode="prep_rel">
    <rel xmlns="livcos.org/ns/map">
      <xsl:apply-templates select="." mode="prep"/>
    </rel>
  </xsl:template>
  
  <xsl:template match="createDate" mode="prep">
    <xsl:attribute name="create">
      <xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + . * xs:dayTimeDuration('PT0.001S')"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="editDate" mode="prep">
    <xsl:attribute name="modify">
      <xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + . * xs:dayTimeDuration('PT0.001S')"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="lastVisitDate" mode="prep">
    <xsl:attribute name="visit">
      <xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + . * xs:dayTimeDuration('PT0.001S')"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="BOOKMARK" mode="prep">
    <item xmlns="livcos.org/ns/map">
      <xsl:attribute name="id">item_<xsl:number level="any" count="CATEGORY | BOOKMARK"/></xsl:attribute>
      <xsl:apply-templates select="createDate | editDate | lastVisitDate" mode="#current"/>
      <xsl:if test="hitCount > 0">
        <xsl:attribute name="hits"><xsl:value-of select="hitCount"/></xsl:attribute>
      </xsl:if>
      <label><xsl:value-of select="name"/></label>
      <xsl:if test="normalize-space(description) != ''">
        <paragraph xmlns="livcos.org/ns/doc" id="paragraph_{count(preceding::description)}">
          <xsl:value-of select="description"/>
        </paragraph>
      </xsl:if>
      <link xmlns="livcos.org/ns/doc" id="link_{count(preceding::url)}" ref="{url}"></link>
      <rels group="sys">
        <rel/>
      </rels>
    </item>
  </xsl:template>
  
</xsl:stylesheet>
